package com.stroke.core.helpers;

import com.stroke.util.RenderingHintsFactory;
import com.stroke.entity.Entity;
import com.stroke.graphics.ScreenManager;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.List;

/**
 * Author: Igor Bubelov
 * Date: 3/7/11 10:27 PM
 */
public class RenderHelper {
    public static Graphics2D prepare(ScreenManager manager) {
        Graphics2D g = manager.getGraphics();

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, manager.getScreen().getWidth(), manager.getScreen().getHeight());

        g.setRenderingHints(RenderingHintsFactory.createBest());

        return g;
    }

    public static void renderEntities(List<Entity> entities, Graphics2D g, Rectangle viewArea) {
        for (Entity entity : entities) {
            if (!entity.isVisible()) {
                continue;
            }

            Image image = entity.getImage();

            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);

            Point origin = entity.getOrigin();
            Point2D position = entity.getPosition();

            int leftBound = (int) (position.getX() - origin.getX());
            int topBound = (int) (position.getY() - origin.getY());

            Rectangle imageBounds = new Rectangle(leftBound, topBound, imageWidth, imageHeight);

            if (imageBounds.intersects(viewArea)) {
                int x = (int) (imageBounds.x - viewArea.getX());
                int y = (int) (imageBounds.y - viewArea.getY());

                g.drawImage(image, x, y, null);
            }
        }
    }
}
